<?php

namespace App\Console\Commands;

use App\Library\Logic\InviteLogic;
use App\Library\Logic\SalesRecordLogic;
use App\Library\Logic\ProductSpecLogic;
use App\Library\Logic\ShopOrderGoodsLogic;
use App\Library\Logic\ShopOrderReturnLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Logic\ShopUserPriceWaitingLogic;
use Exception;
use Illuminate\Console\Command;

class ShopOrderReturnCommand extends Command
{
    /**
     * 订单商品退货
     *
     * @var string
     */
    protected $signature = 'command:shopOrderReturn {serialNumber}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * handle
     * @throws Exception
     */
    public function handle(): void
    {
        $serialNumber = $this->argument('serialNumber');
        if (empty($serialNumber) || strlen($serialNumber) !== 20) return;
        $model = ShopOrderReturnLogic::findBySerialNumber($serialNumber);
        if (is_null($model) || $model->or_api_status !== 'SUCCESS') return;
        else if ($model->order->ext->refund_script === 1) return;
        $model->order->ext->status_refund = 1;
        $model->order->ext->refund_at = now()->timestamp($model->or_api_time);
        $model->order->ext->refund_price = $model->or_real_price;
        if ($model->order->ext->status_deliver === 0) {
            ProductSpecLogic::incStock($model->order_id);
            $model->order->ext->status_inventory = 0;
        }
        $model->order->ext->status = $model->or_type === 2 ? 5 : 6;
        $model->order->ext->refund_script = 1;
        $model->order->ext->save();
        $model->order->payment->status_refund = 1;
        $model->order->payment->save();
        $model->order->paymentRelated->status_refund = 1;
        $model->order->paymentRelated->save();
        $where = ['objectType' => $model->order->getTable(), 'objectId' => $model->order_id];
        if ($model->or_type !== 2 && bcsub($model->or_price, $model->or_ensure_price * 1, 2) !== bcsub($model->or_real_price, 0, 2)) {
            $commission = ShopOrderGoodsLogic::sumByWhere('orderId', $model->order_id, 'commission');
            $amount = bcsub(bcsub($model->or_price, $model->or_real_price, 2), $model->or_ensure_price, 2);
            SalesRecordLogic::updateByWhere(['amount' => bcsub($amount, $commission, 2)], $where);
            $where = array_merge($where, ['type' => 'invite']);
            $record = SalesRecordLogic::getByWhere($where);
            foreach ($record as $item) {
                $invite = InviteLogic::findByWhere(['inviterId' => $item->to_user_id, 'inviteeId' => $item->from_user_id]);
                if (!is_null($invite)) {
                    $invite->commission -= $item->amount;
                    $invite->save();
                }
            }
        }
        SalesRecordLogic::deleteByWhere($where);
        mq('shopOrderSettlement', $model->order->serial_number);
        ShopProductLogic::saveSale($model->order_id, 1);
    }
}
